import os
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
django.setup()

from faker import Faker
from doc_manager.models import Document 
import random
from django.core.files import File

fake = Faker('zh_CN')

def generate_mock_documents(count=10):
    doc_types = ['pdf', 'doc', 'xls', 'ppt', 'img', 'other']
    categories = ['finance', 'legal', 'hr', 'tech', 'marketing', 'other']
    
    for _ in range(count):
        doc = Document(
            title=fake.sentence(),
            doc_type=random.choice(doc_types),
            version=f"{random.randint(1, 5)}.{random.randint(0, 9)}",
            category=random.choice(categories)
        )
        
        # 创建虚拟文件
        file_name = f"{fake.word()}.{doc.doc_type if doc.doc_type != 'img' else 'jpg'}"
        file_path = os.path.join('documents', file_name)
        with open(file_path, 'w') as f:
            f.write(fake.text())
        
        with open(file_path, 'rb') as f:
            doc.file.save(file_name, File(f))
        
        doc.save()
        os.remove(file_path)
        
        print(f"Created document: {doc.title}")

if __name__ == "__main__":
    generate_mock_documents(20)